package oen.game.unpack.tools.handler;

import im.oen.boot.common.utils.Checker;
import im.oen.boot.common.utils.Runner;
import oen.game.unpack.tools.handler.netease.NpkFileHandler;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>描述：暂无描述信息</p>
 *
 * <p>创建时间：2021-11-10 18:08</p>
 * <p>更新时间：暂无</p>
 *
 * @author Kevin.Xu
 * @version 1.0
 */
public class FileProcessor {

    private static FileProcessor processor = new FileProcessor();

    public static FileProcessor instance() {
        return processor;
    }

    private List<FileHandler> handlers = new ArrayList<>();

    private FileProcessor() {
        handlers.add(new NpkFileHandler());
    }

    public void process(final String dir) throws Exception {
        if (!new File(dir).exists()) {
            return;
        }

        final Set<File> files = Files.list(Path.of(dir)).map(Path::toFile).filter(File::isFile).collect(Collectors.toSet());
        if (Checker.isEmpty(files)) {
            return;
        }

        for (FileHandler fileHandler : handlers) {
            Runner.safeRun(() -> {
                Set<File> supports = fileHandler.fetchFiles(files);
                fileHandler.process(supports);
            });
        }
    }
}
